﻿Imports System.Data
Imports VSModel
Imports AutoDevModel
Imports Cls_Common
Imports SecurityService
Partial Class Control_TiepNhanMCC_XuLyChamDut
    Inherits System.Web.UI.UserControl
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
#Region "Sub and Function "
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim script As ScriptManager = ScriptManager.GetCurrent(Me.Page)
            If Not script Is Nothing AndAlso script.IsInAsyncPostBack Then
                ScriptManager.RegisterStartupScript(Me.Page, Me.Page.GetType, "jqueryEnable", "ajaxJquery()", True)
            Else
                Me.Page.ClientScript.RegisterStartupScript(Me.Page.GetType(), "jqueryEnable", String.Concat("Sys.Application.add_load(function(){", "ajaxJquery()", "});"), True)
            End If
            If Session("UserId") Is Nothing Then
                Response.Redirect("~/Login.aspx")
            End If

            If Not Request.QueryString("Id").ToString.Equals("0") Then
                hidCurHuyenId.Value = Session("HuyenId")
                hidIDMCC.Value = Request.QueryString("Id")
                Dim _CMND As String = Request.QueryString("CMND")
                DivCheckCertifiedCode.Visible = False
                ShowDetailCertifiedCode.Visible = False
                ThongBao.Visible = False
                Using _data As New VSEntities
                    Dim _CertifiedID = (From i In _data.CertifiedHouseholds Where i.ProvinceId = hidCurHuyenId.Value And ((i.Ow_CardID <> "" And i.Ow_CardID = _CMND) Or (i.CertifiedCode <> "" And i.CertifiedCode = _CMND)) Order By i.CertifiedDate Descending Select i).FirstOrDefault
                    If Not _CertifiedID Is Nothing Then
                        hidID.Value = IIf(IsNothing(_CertifiedID.CertifiedID) = True, "", _CertifiedID.CertifiedID)
                        ShowDetailCertifiedCode.Visible = True
                    Else
                        DivCheckCertifiedCode.Visible = True
                    End If
                End Using
                ShowData()
                ShowDataChamDut()
                LoadMotCua()
            End If
        End If
    End Sub
    Protected Sub ShowDataChamDut()
        Using data As New VSEntities
            Dim p = (From q In data.CertifiedChamDuts Where q.CertifiedID = hidID.Value Select q).FirstOrDefault
            If Not p Is Nothing Then
                If Not p.NgayChamDut Is Nothing AndAlso p.NgayChamDut <> "01/01/1900" Then
                    txtNgayChamDut.Text = CType(p.NgayChamDut, Date).ToString("dd/MM/yyyy") 'ngay chấm dứt
                End If
                txtGhiChu.Text = IIf(IsNothing(p.GhiChu) = True, "", p.GhiChu) 'ghi chú
            End If
        End Using

    End Sub
    Protected Sub ShowData()
        Using data As New VSEntities
            Dim _curHuyenId As Integer = Session("HuyenId")
            Dim p = (From q In data.CertifiedHouseholds
                     Where q.CertifiedID = hidID.Value And q.ProvinceId = _curHuyenId
                     Order By q.CertifiedUp_Date Descending Select q).FirstOrDefault
            If Not p Is Nothing Then
                hidID.Value = IIf(IsNothing(p.CertifiedID) = True, "", p.CertifiedID)
                hidCertifiedCode.Value = IIf(IsNothing(p.CertifiedCode) = True, "", p.CertifiedCode)
                'Tên người đăng ký kinh doanh
                txtBiz_VietNam.Text = IIf(IsNothing(p.Biz_VietName) = True, "", p.Biz_VietName)
                'Mã cấp giấy chứng nhận
                txtCertifiedCode.Text = IIf(IsNothing(p.CertifiedCode) = True, "", p.CertifiedCode)
                'Tên văn phòng cấp giấy chứng nhận
                txtCertifiedDeptName.Text = IIf(IsNothing(p.CertifiedDeptName) = True, "", p.CertifiedDeptName)
                'Ngày cấp giấy chứng nhận
                If Not p.CertifiedDate Is Nothing Then
                    txtCertifiedDate.Text = CType(p.CertifiedDate, Date).ToString("dd/MM/yyyy")
                End If
                'địa điểm kinh doanh
                txtBiz_HeadOffice.Text = IIf(IsNothing(p.Biz_VietName) = True, "", p.Biz_VietName)
                'điên thoại
                txtOw_Tel.Text = IIf(IsNothing(p.Ow_Tel) = True, "", p.Ow_Tel)
                'fax
                txtOw_Fax.Text = IIf(IsNothing(p.Ow_Fax) = True, "", p.Ow_Fax)
                'Email
                txtOw_Email.Text = IIf(IsNothing(p.Ow_Email) = True, "", p.Ow_Email)
                'website
                txtOw_WebSite.Text = IIf(IsNothing(p.Ow_WebSite) = True, "", p.Ow_WebSite)
            End If
        End Using
    End Sub
    Protected Sub LoadMotCua()
        Using _mc As New AutoDevEntities
            Dim _motcua = (From q In _mc.HoSoes Where q.Id = hidIDMCC.Value Select q).FirstOrDefault
            If Not _motcua Is Nothing Then
                'biên nhận từ MCC
                lblBienNhan.Text = _motcua.SoBienNhan
                lblSoBienNhanTuMcc.Text = _motcua.SoBienNhan
            End If
        End Using
    End Sub
    Protected Function Save() As Boolean
        Using data As New VSEntities
            Dim _checkGCN = (From q In data.CertifiedChamDuts Where q.CertifiedID = hidID.Value Select q).FirstOrDefault
            If Not _checkGCN Is Nothing Then
                Return False
                Exit Function
            End If
            Dim p As New VSModel.CertifiedChamDut
            Try
                p.CertifiedID = hidID.Value
                p.CertifiedCode = hidCertifiedCode.Value
                p.NgayChamDut = StringToDate(txtNgayChamDut.Text.Trim())
                p.GhiChu = txtGhiChu.Text.Trim()
                p.StatusId = TinhTrangHoSo.Nhap
                p.Nguoitao = Session("UserName")
                p.NgayTao = Now
                p.HuyenId = Session("HuyenId")
                data.CertifiedChamDuts.AddObject(p)
                Dim strLog As String = "Giay CN:" & txtCertifiedCode.Text.Trim() & ", tên hộ: " & txtBiz_VietNam.Text.Trim()
                Insert_App_Log("Insert  Chấm dứt kinh doanh:" & strLog & "", Function_Name.CertifiedChamDut, Audit_Type.Create, Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
                data.SaveChanges()
                SaveBienNhan()
                Dim _huyenId As Integer = Session("HuyenId")
                data.uspCertifiedChamDutUpdate(hidCertifiedCode.Value, _huyenId, 1)
                Return True
            Catch ex As Exception
                log4net.Config.XmlConfigurator.Configure()
                log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                Return False
            End Try
        End Using
    End Function
    Protected Sub SaveBienNhan()
        Using data As New VSEntities
            Dim p = (From q In data.CertifiedHouseholds Where q.CertifiedID = hidID.Value Select q).FirstOrDefault
            If Not p Is Nothing Then
                Try
                    p.MaBienNhan = lblBienNhan.Text.Trim()
                    Dim strLog As String = "Giay CN:" & txtCertifiedCode.Text.Trim() & ", tên hộ: " & txtBiz_VietNam.Text.Trim() & ",Mã biên nhận từ MCC" & lblBienNhan.Text.Trim()
                    Insert_App_Log("Sửa  Certified:" & strLog & "", Function_Name.CertifiedHousehold, Audit_Type.Edit, Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
                    data.SaveChanges()
                Catch ex As Exception
                    log4net.Config.XmlConfigurator.Configure()
                    log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                    Excute_Javascript("Alertbox('Cập nhật thất bại.');window.location ='../../Page/TiepNhan/MotCua.aspx';", Me.Page, True)
                End Try
            End If
        End Using
    End Sub
    Protected Sub ResetControl()
        txtNgayChamDut.Text = ""
        txtGhiChu.Text = ""
    End Sub
#End Region
#Region "Event for control "
    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If Save() Then
            Using data As New VSEntities
                Dim p = (From q In data.CertifiedChamDuts Where q.CertifiedID = hidID.Value Select q).FirstOrDefault
                If Not p Is Nothing Then
                    Excute_Javascript("Alertbox('Thêm mới dữ liệu thành công.');window.location ='../../Page/CertifiedChamDut/Detail.aspx?Id=" & p.Id & "';", Me.Page, True)
                End If
            End Using
        Else
            Excute_Javascript("Alertbox('GCN đã chấm dứt trước đó .');", Me.Page, True)
        End If
    End Sub
    Protected Sub btnSaveAndNew_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSaveAndNew.Click
        If Save() Then
            Excute_Javascript("Alertbox('Cập nhật dữ liệu thành công.');", Me.Page, True)
            ResetControl()
        Else
            Excute_Javascript("Alertbox('Cập nhật thất bại.');", Me.Page, True)
        End If
    End Sub
    Protected Sub btnReset_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReset.Click
        Response.Redirect(MainDomain & "Page/TiepNhan/MotCua.aspx")
    End Sub
    Protected Sub btnBack_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnBack.Click
        Response.Redirect(MainDomain & "Page/TiepNhan/MotCua.aspx")
    End Sub
#End Region


    Protected Sub btnCheckCertifiedCode_Click(sender As Object, e As EventArgs) Handles btnCheckCertifiedCode.Click
        Using _data As New VSEntities
            Dim p = (From q In _data.CertifiedHouseholds Where (q.CertifiedCode = txtCertifiedcodechek.Text.Trim() Or q.Ow_CardID = txtCertifiedcodechek.Text.Trim()) _
                                            And q.ProvinceId = hidCurHuyenId.Value _
                                            And q.CertifiedCode <> ""
                                            Order By q.CertifiedDate Descending Select q).FirstOrDefault
            Dim parameter As String = ""
            If Not p Is Nothing Then
                If Not p.Ow_CardID Is Nothing And p.Ow_CardID <> "" Then
                    parameter = p.Ow_CardID
                ElseIf Not p.CertifiedCode Is Nothing And p.CertifiedCode <> "" Then
                    parameter = p.CertifiedCode
                End If
                Response.Redirect(MainDomain & "Page/QuanLyHoSo/MotCuaImportChamDut.aspx?Id=" & hidIDMCC.Value & "&CMND=" & parameter)
            Else
                hplCapMoi.NavigateUrl = MainDomain & "Page/TiepNhan/MotCua.aspx"
                DivCheckCertifiedCode.Visible = True
                ShowDetailCertifiedCode.Visible = False
                ThongBao.Visible = True

            End If
        End Using
    End Sub
End Class
